const list2Tree = (list: any[], field: TreeFieldType, deletePid?: boolean): any[] => {
  // 空数组
  let result: any = [];
  // 判断不是数组  直接返回
  if (!Array.isArray(list)) {
    return result;
  }
  // 遍历  删除  children 属性  做初始化操作
  list.forEach((item: any) => {
    delete item.children;
  });
  //  空对象
  let map = {};
  list.forEach((item: any) => {
    item.key = item[field.id];
    map[item[field.id]] = item;
  });
  list.forEach((item: any) => {
    item.key = item[field.id];
    // item.pid 为0时 返回underfined
    let parent = map[item[field.parentId]];
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      // 这里push的item是pid为0的数据
      result.push(item);
    }
    if (deletePid) {
      delete item.parentId
    }
  });
  return result;
};


export default { list2Tree };
